{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "来自模块 hello： 导入模块\n",
      "total =  30\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "30"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import support_model\n",
    "\n",
    "support_model.print_func('导入模块')\n",
    "support_model.sumNumber(10,20)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### from…import 语句\n",
    "####  Python 的 from 语句让你从模块中导入一个指定的部分到当前命名空间中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "total =  100\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "100"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from support_model2 import sumNumber\n",
    "sumNumber(40,60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "来自模块 hello： 模块二\n"
     ]
    }
   ],
   "source": [
    "from support_model2 import print_func\n",
    "print_func('模块二')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "\n",
    "from…import* 语句\n",
    "把一个模块的所有内容全都导入到当前的命名空间也是可行的，只需使用如下声明：\n",
    "\n",
    "from modname import *\n",
    "这提供了一个简单的方法来导入一个模块中的所有项目。然而这种声明不该被过多地使用。\n",
    "\n",
    "例如我们想一次性引入 math 模块中所有的东西，语句如下：\n",
    "\n",
    "from math import *\n",
    "\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 命名空间和作用域"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "一个 Python 表达式可以访问局部命名空间和全局命名空间里的变量。如果一个局部变量和一个全局变量重名，则局部变量会覆盖全局变量。\n",
    "\n",
    "每个函数都有自己的命名空间。类的方法的作用域规则和通常函数的一样。\n",
    "\n",
    "Python 会智能地猜测一个变量是局部的还是全局的，它假设任何在函数内赋值的变量都是局部的。\n",
    "s\n",
    "因此，如果要给函数内的全局变量赋值，必须使用 global 语句。\n",
    "\n",
    "global VarName 的表达式会告诉 Python， VarName 是一个全局变量，这样 Python 就不会在局部命名空间里寻找这个变量了。\n",
    "\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加之前的 Money =  100\n",
      "添加之后的 Money =  300\n"
     ]
    }
   ],
   "source": [
    "Money = 100\n",
    "\n",
    "def AddMoney():\n",
    "    global Money # 不打开会报错： local variable 'Money' referenced before assignment\n",
    "    Money = Money + 200\n",
    "    return Money\n",
    "\n",
    "    \n",
    "print('添加之前的 Money = ',Money)\n",
    "AddMoney()\n",
    "print('添加之后的 Money = ',Money)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python中的包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "\n",
    "Python中的包\n",
    "包是一个分层次的文件目录结构，它定义了一个由模块及子包，和子包下的子包等组成的 Python 的应用环境。\n",
    "\n",
    "简单来说，包就是文件夹，但该文件夹下必须存在 __init__.py 文件, 该文件的内容可以为空。__init__.py 用于标识当前文件夹是一个包\n",
    "\n",
    "\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm in runoob1\n",
      "I'm in runoob12\n"
     ]
    }
   ],
   "source": [
    "from package_runoob.runoob1 import runoob1\n",
    "from package_runoob.runoob2 import runoob2\n",
    "runoob1()\n",
    "runoob2()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
